﻿var SPListKit = window.SPListKit || {};

SPListKit.RibbonGroupLeafId = "SPListKit";

SPListKit.RibbonResources = {};

SPListKit.RibbonInitOptions = { localize: false };

SPListKit.overrideRibbonInitFunc = function () {
    if (!window.g_original_ribbonInitFunc1 && typeof (window._ribbonInitFunc1) == "function") {
        g_original_ribbonInitFunc1 = window._ribbonInitFunc1;
        window._ribbonInitFunc1 = function () {
            g_original_ribbonInitFunc1();
            SPListKit.initRibbon();
        };
    }
};

SPListKit.initRibbon = function () {
    if (typeof (_ribbon) == "undefined") return;
    if (!SPListKit.RibbonInitOptions.localize) return;

    var newDataExtensions = g_builder.get_ribbonBuildOptions().dataExtensions;
    var groupsChildrenKey = ".Groups._children";
    var controlsChildrenKey = ".Controls._children";
    for (var extensionKey in newDataExtensions) {
        if (SPListKit.endsWith(extensionKey, groupsChildrenKey)) {
            var groups = newDataExtensions[extensionKey];
            var controls = null;
            for (var groupIndex = 0; groupIndex < groups.length; groupIndex++) {
                // get 'SP List Kit' group
                var group = groups[groupIndex];
                if (group.name != "Group") continue;
                if (!SPListKit.endsWith(group.attrs.Id, SPListKit.RibbonGroupLeafId)) continue;
                var controlsExists = false;
                var groupChild = null;
                for (var childIndex = 0; childIndex < group.children.length; childIndex++) {
                    groupChild = group.children[childIndex];
                    if (groupChild.name == "Controls") {
                        controlsExists = (groupChild.children.length > 0);
                        break;
                    }
                }
                if (!controlsExists) continue;

                SPListKit.localizeRibbonControls(groupChild.children);
            }
        } else if (SPListKit.endsWith(extensionKey, controlsChildrenKey)) {
            SPListKit.localizeRibbonControls(newDataExtensions[extensionKey]);
        }
    }
};

SPListKit.localizeRibbonControls = function (controls) {
    for (var controlIndex = 0; controlIndex < controls.length; controlIndex++) {
        var control = controls[controlIndex];
        if (!SPListKit.RibbonResources[control.attrs.Id]) continue;

        var res = SPListKit.RibbonResources[control.attrs.Id];
        if (res.LabelText) {
            control.attrs.LabelText = res.LabelText;
        }
        if (res.ToolTipTitle) {
            control.attrs.ToolTipTitle = res.ToolTipTitle;
        }
        if (res.ToolTipDescription) {
            control.attrs.ToolTipDescription = res.ToolTipDescription;
        }
    }
};

SPListKit.localizeStaticRibbonControl = function (key) {
    // require jquery
    var control = $("[id|='" + key + "']");
    if (!control.length) return;
    var label = control.children("span[class$='label']"); // doesn't work if using .find()
    label.html(SPListKit.RibbonResources[key].LabelHtml);
};

SPListKit.endsWith = function (text, subText) {
    return text.indexOf(subText) == (text.length - subText.length);
};
